package uia.is202.jpademo.web;

import java.io.*;
import java.util.Enumeration;
import javax.servlet.*;
import javax.servlet.http.*;


/** A simple servlet that returns a page with all
 *  the details of the request. Very useful for
 *  debugging. */
public class TestServlet extends HttpServlet {
    public static final long serialVersionUID = 1;
    

    /**
     * Handle get requests by calling returnRequest()
     *
     * @param req the request object.
     * @param res the response object
     *
     * @throws IOException maybe...
     * @throws ServletException maybe...
     */
    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse res)
            throws IOException, ServletException {
        returnRequest(req, res);
    }


    /**
     * Handle post requests by calling returnRequest()
     *
     * @param req the request object.
     * @param res the response object
     *
     * @throws IOException maybe...
     * @throws ServletException maybe...
     */
    @Override
    public void doPost(HttpServletRequest req, HttpServletResponse res)
            throws IOException, ServletException {
        returnRequest(req, res);
    }


    /**
     * Handle any request by return a html page with details about the request
     *
     * @param req the request object.
     * @param res the response object
     *
     * @throws IOException maybe...
     * @throws ServletException maybe...
     */
    public void returnRequest(HttpServletRequest req, HttpServletResponse res)
            throws IOException, ServletException {
        res.setContentType("text/html");
        res.setHeader("Cache-control", "no-cache");
        res.setHeader("Expires", "0");

        PrintWriter out = res.getWriter();
        String uri = req.getRequestURI();
        String contextPath = req.getContextPath();
        String servletPath = req.getServletPath();
        String pathInfo = req.getPathInfo();
        String contentType = req.getContentType();

        // Write html header
        out.println("<html>\n<head>\n<title>Employee List</title>\n</head>");
        out.println("<body>");
        out.println("<h3>Request Info:</h3>");
        out.println("Method: " + req.getMethod() + "<br>");
        out.println("ContentType: "+contentType+"<br>");
        out.println("Request URI: " + uri + "<br>");
        out.println("Protocol: " + req.getProtocol() + "<br>");
        out.println("Context path: " + contextPath + "<br>");
        out.println("Servlet path: " + servletPath + "<br>");
        out.println("PathInfo: " + pathInfo + "<br>");
        out.println("Remote Address: " + req.getRemoteAddr() + "<p>");

        out.println("Parameters:<br>");

        if (true) {
            Enumeration e = req.getParameterNames();

            while (e.hasMoreElements()) {
                String name = (String)e.nextElement();
                String value[] = req.getParameterValues(name);
                out.print(name + " =");
                for (int i=0; i<value.length; i++) out.print(" "+value[i]);
                out.println("<br>");
            }
        }

        out.println("\n</body>\n</html>");
    }
}
